<?php

class Group_controller extends CI_Controller{

    function __construct(){
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->helper('utils');
                            
        Validator::is_access($this->session->userdata('user_id'));
    }

    function index(){

        $section = "group";
        $title = _LB_GROUP;
        $cfer = new Cfer(array(
        $title => base_url('group')));
        
        $filter = array();
        $filter['name'] = $this->input->post('name');
        $group = Group::getList($filter);

        $this->data['group'] = $group;
        $this->data['filter'] = $filter;
        $this->data['title'] = $title;
        

        $array_menus = array();
        
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);
        $this->data['cfer'] = $cfer;
        $this->data['array_menus'] = $array_menus;
        $this->data['section'] = $section;
        $this->data['cfer'] = $cfer;
        $this->load->view('group_list', $this->data);
    }

    function delete($id = null) {
        $group = new Group();

        if (!$id || !$group->get($id) || $id == ADMIN) {
                redirect(base_url('group'));
        }
        $group->delete();
        redirect(base_url('group'));
    }
    
    function form($id = null) {
        $section = "group";
		
        if ($id) {
            $cfer = new Cfer(array(
            _LB_GROUP => 'group',
            _LB_EDIT_GROUP => base_url('group/form/'.$id)));
        } else {
            $cfer = new Cfer(array(
            _LB_GROUP => 'group',
            _LB_ADD_GROUP => base_url('group/form/')));
        }
        
        $act = $this->input->get_post('act');
    
        $group = new Group();

        if ($id && !$group->get($id)) {
                redirect(base_url('group'));
        }
        
        if ($act == ACT_VALID_EDIT) {

      
            if($id != ADMIN) {
                $group->allow_access = $this->input->post('allow_access');
            }
            $group->name = $this->input->post('name');
            if ($group->validateInput()) {
                if ($id) {
                    $group->update();
                } else {
                    $group->insert();
                }
                redirect(base_url('group'));
            }
        }

        $array_menus = array();
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);
        
        $title = _LB_GROUP;
        
        $this->data['group'] = $group;
        $this->data['title'] = $title;
        $this->data['cfer'] = $cfer;
        $this->data['array_menus'] = $array_menus;
        $this->data['backlink'] = base_url('group');
        $this->data['section'] = $section;
        $this->data['cfer'] = $cfer;
        $this->load->view('group_form', $this->data);
    }

}
